package com.hn658.groupshare.frontend.action.upload.impl;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import org.apache.cxf.jaxrs.ext.multipart.Multipart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.hn658.framework.common.AbstractBaseService;
import com.hn658.framework.common.IServiceResponse;
import com.hn658.framework.shared.exception.BusinessException;
import com.hn658.groupshare.filemgmt.service.IFileManager;
import com.hn658.groupshare.filemgmt.service.IValidator;
import com.hn658.groupshare.filemgmt.service.exception.FileException;
import com.hn658.groupshare.frontend.action.account.IUserAccountAction;
import com.hn658.groupshare.frontend.action.upload.vo.UploadResponse;

/**
 * 图片上传
 * 
 * @ClassName: UploadActionImpl
 * @author ztjie
 * @date 2015年1月26日 上午11:46:00
 */
@Controller("uploadAction")
@Path("upload")
@Produces("application/json; charset=UTF-8")
@Consumes("application/json; charset=UTF-8")
public class UploadActionImpl extends AbstractBaseService implements
		IUserAccountAction {

	@Autowired
	private IFileManager fileManager;
	
	@Autowired
	private IValidator validator;

	@Path("qrCodeImage")
	@Consumes("multipart/form-data; charset=UTF-8")
	@POST
	public IServiceResponse qrCodeImage(
			@Multipart(value = "qrcodeFile", required = false) byte[] file,
			@Context HttpServletRequest request) {
		UploadResponse response = new UploadResponse();
		try {
			if(file==null){
				throw new FileException(FileException.UploadFileIsEmpty);
			}
			
			String[] uris = fileManager.saveQrCodeImage(file,validator);
			
			response.setFileUrl(uris[0]);
			response.setQrcodeText(uris[1]);
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
	
	
	@Path("managerQrCode")
	@Consumes("multipart/form-data; charset=UTF-8")
	@POST
	public IServiceResponse managerQrCode(
			@Multipart(value = "managerQrCodeFile", required = false) byte[] file,
			@Context HttpServletRequest request) {
		UploadResponse response = new UploadResponse();
		try {
			if(file==null){
				throw new FileException(FileException.UploadFileIsEmpty);
			}
			
			String[] uris = fileManager.saveQrCodeImage(file,null);
			
			response.setFileUrl(uris[0]);
			response.setQrcodeText(uris[1]);
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
	
	@Path("avatarImage")
	@Consumes("multipart/form-data; charset=UTF-8")
	@POST
	public IServiceResponse avatarImage(
			@Multipart(value = "imageFile", required = false) byte[] file,
			@Context HttpServletRequest request) {
		UploadResponse response = new UploadResponse();
		try {
			if(file==null){
				throw new FileException(FileException.UploadFileIsEmpty);
			}
			String[] uris = fileManager.saveAvatarImage(file);
			response.setFileUrl(uris[0]);
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}

}
